<template>
  <el-dialog title="设置起算日" :visible.sync="$parent.opendetailed" width="75%">
    <el-table :data="noticeList" v-loading="loading">
      <el-table-column label="序号" align="center" prop="order" width="50" />
      <el-table-column label="设备编号" align="center" min-width="130">
        <template slot-scope="scope">
          {{ scope.row.equipmentNum || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" min-width="130">
        <template slot-scope="scope">
          {{ scope.row.equipmentName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" min-width="130">
        <template slot-scope="scope">
          {{ scope.row.brand || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" min-width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.model || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" min-width="150" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.supplierName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.unitName || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.remark || '-' }}
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="220">
        <template slot="header">
          <span style="color:red">*</span>{{ rowObj.cycleType === '0' ? `循环起算日` : `指定保养日期` }}
          <!-- 循环起算日 -->
        </template>
        <template slot-scope="scope">
          <!-- {{ scope.row.specifyTime || '-' }} -->
          <el-date-picker v-model="scope.row.specifyTime" type="date" style="width:217px" value-format="yyyy-MM-dd"
            :placeholder="`请选择${rowObj.cycleType === '0' ? `循环起算日` : `指定保养日期`}`" />
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="card-div">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div> -->
    <div align="center" slot="footer" class="dialog-footer">
      <el-button @click="() => $parent.opendetailed = false">关闭</el-button>
      <el-button type="success" @click="CommitPush()">启用</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getSelectChildrenPlanEquipment, putUpdateplanChangeStatus, putupkeepSpecifyTime } from '@/api/maintenance/insurance/insurance.js'
import { formatDate } from '@/utils/index.js'
export default {
  props: ['rowObj'],
  data () {
    return {
      ObjData: {},
      eaPartInfoVo: {},
      loading: false,
      noticeList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'equipmentAccountId',
        isAsc: 'desc'//倒叙
      },
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /**
     * 初始化
     */
    async getList () {
      let params = {
        // ...this.queryParams,
        'params[scope1]': 1,
        'params[scope4]': 1,
        'params[upkeepPlanId]': this.rowObj.upkeepPlanId //保养计划ID
      }
      this.loading = true
      let data = await getSelectChildrenPlanEquipment(params)
      let current = formatDate(new Date())
      this.noticeList = data && data.data.map((item, index) => {
        return {
          ...item,
          order: index + 1,
          specifyTime: item.specifyTime < current ? current : item.specifyTime
        }
      })
      // this.total = data.total
      this.loading = false
    },
    /**
     * 启用按钮
     */
    async CommitPush () {
      for (let index = 0; index < this.noticeList.length; index++) {
        const element = this.noticeList[index];
        if (!element.specifyTime) {
          return this.$message.error('须填写所有循环起算日')
        }
      }

      let params = {
        upkeepPlanId: this.rowObj.upkeepPlanId,
        status: 0
      }
      let ArrData = this.noticeList.map(item => {
        return {
          upkeepPlanId: this.rowObj.upkeepPlanId,
          equipmentAccountId: item.equipmentAccountId,
          specifyTime: item.specifyTime
        }
      })
      await putUpdateplanChangeStatus(params)//修改状态
      await putupkeepSpecifyTime(ArrData)//修改循环起算日
      this.$parent.opendetailed = false//关闭窗口
      this.$parent.getList()//更新列表数据
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .descript .is-bordered {
  table-layout: fixed
}
</style>